<template>
  <div>
    <Card style="margin-bottom: 10px;" >
      <BarMulti
        :chartData="barMultiData"
        :option="barOptions"
        height="40vh"
      />
    </Card>
    
    <BasicTable @register="registerTable" :rowSelection="rowSelection">
      <template #tableTitle>
        <a-button preIcon="ant-design:clear-outlined" type="primary" @click="handleClean" style="margin-right: 5px"
          danger>清空</a-button>
        <a-button type="primary" preIcon="ant-design:export-outlined" @click="onExportXls"> 导出</a-button>
        <j-upload-button type="primary" preIcon="ant-design:import-outlined" @click="onImportXls">导入</j-upload-button>
        <a-dropdown v-if="selectedRowKeys.length > 0">
          <template #overlay>
            <a-menu>
              <a-menu-item key="1" @click="batchHandleDelete">
                <Icon icon="ant-design:delete-outlined"></Icon>
                删除
              </a-menu-item>
            </a-menu>
          </template>
          <a-button>批量操作
            <Icon icon="mdi:chevron-down"></Icon>
          </a-button>
        </a-dropdown>
      </template>
      <template #action="{ record }">
        <TableAction :actions="getActions(record)" />
      </template>
    </BasicTable>
    <Detail @register="registerDesc"></Detail>
  </div>
</template>
<script lang="ts" name="monitor-access-log" setup>
import { ref, Ref, onMounted } from 'vue';
import { Card } from 'ant-design-vue';
import { BasicTable, TableAction } from '/@/components/Table';
import BarMulti from '/@/components/chart/BarMulti.vue';
import { useDrawer } from '/@/components/Drawer';
import { useListPage } from '/@/hooks/system/useListPage'
import * as logApi from './api'
import * as eventApi from '../logEvent/api'
import { columns, searchFormSchema } from './data';
import Detail from './Detail.vue';
import { useMessage } from '/@/hooks/web/useMessage';
import { Icon } from '/@/components/Icon';
import { queryStatistic } from '/@/api/index';

const { createMessage } = useMessage();
const [registerDesc, { openDrawer: openLogDesc }] = useDrawer();

const { prefixCls, tableContext, onExportXls, onImportXls } = useListPage({
  designScope: 'access-log',
  tableProps: {
    title: '访问日志列表',
    api: logApi.getLogList,
    columns: columns,
    bordered: false,
    canResize: false,
    rowSelectionTipType: 'onTableTitle',
    actionColumn: {
      width: 130,
      title: '操作',
      dataIndex: 'action',
      fixed: false,
      slots: { customRender: 'action' },
    },
    formConfig: {
      labelWidth: 120,
      schemas: searchFormSchema,
      fieldMapToTime: [['fieldTime', ['beginDate', 'endDate'], 'YYYY-MM-DD HH:mm:ss']],
    },
  },
  exportConfig: {
    name: "操作日志列表",
    url: logApi.getExportUrl,
  },
  importConfig: {
    url: logApi.getImportUrl
  }
})

const [registerTable, { reload, getForm }, { rowSelection, selectedRowKeys }] = tableContext;

const barMultiData = ref<any[]>([]);

const barOptions = ref({
  title: { text: '周统计', textStyle: { fontWeight: 'lighter' } },
  series: [
    {
      smooth: true,
      type: '正常',
      areaStyle: {},
      itemStyle: {
        color: '#2494fc',
      },
    },
    {
      smooth: true,
      type: '错误',
      areaStyle: {},
      itemStyle: {
        color: '#f42c3c',
      },
    },
  ],
});

onMounted(async () => {
  barMultiData.value = await queryStatistic({ days: 7, biz: 'monitor', method: 'access' })

  const { updateSchema } = getForm()
  updateSchema({
    field: 'eventObjectKey',
    componentProps: ({ }) => {
      return {
        optionFilterProp: "label",
        labelField: 'eventValue',
        valueField: 'eventKey',
        showSearch: true,
        allowClear: true,
        api: eventApi.listLogEvent,
        params: {
          parentKey: '0'
        },
        onSelect: async (value: any, option: any) => {
          console.log("value", value);
          updateEventTypeSchema(value)
          reload()
        }
      }
    },
  })
})

const updateEventTypeSchema = (eventObjectKey) => {
  const { updateSchema } = getForm()
  updateSchema({
    field: 'eventTypeKey',
    componentProps: ({ }) => {
      return {
        optionFilterProp: "label",
        labelField: 'eventValue',
        valueField: 'eventKey',
        allowClear: true,
        showSearch: true,
        api: eventApi.listLogEvent,
        params: {
          parentKey: eventObjectKey
        },
        onSelect: async (value: any, option: any) => {
          reload()
        }
      }
    },
  })
}

/**
 * 操作列定义
 * @param record
 */
function getActions(record) {
  return [
    {
      label: '详情',
      onClick: handleDesc.bind(null, record),
    },
    {
      label: '删除',
      color: 'error',
      popConfirm: {
        title: '是否确认删除',
        confirm: handleDelete.bind(null, record),
      }
    }
  ];
}

/**
 * 清空
 */
function handleClean() {
  logApi.cleanLog(reload)
}

/**
 * 详情
 */
function handleDesc(record) {
  openLogDesc(true, {
    record,
    isUpdate: true,
  });
}

/**
 * 删除事件
 */
async function handleDelete(record) {
  await logApi.deleteLog({ id: record.id }, reload);
}


/**
 * 批量删除事件
 */
async function batchHandleDelete() {
  console.log("selectedRowKeys: ", selectedRowKeys.value);
  await logApi.batchDeleteLog({ ids: selectedRowKeys.value }, reload);
}
</script>
